package com.zhouchao.persistence.session;

import com.zhouchao.persistence.exception.BizException;
import com.zhouchao.persistence.pojo.Configuration;
import com.zhouchao.persistence.pojo.MappedStatement;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @Author: zhouchao
 * @Date: 2021/05/03 20:59
 * @Description:
 */
public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <T> List<T> selectList(String statementId, Object... params) throws Exception {

        MappedStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);

        //调用simpleExecutor的query方法
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        List<T> list = simpleExecutor.query(configuration, mapperStatement, params);

        return list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<T> list = selectList(statementId, params);
        if (list.size() != 1) {
            throw new RuntimeException("结果返回异常");
        }
        return list.get(0);
    }

    @Override
    public int insert(String statementId, Object params) throws Exception {
        return update(statementId, params);
    }

    @Override
    public int update(String statementId, Object params) throws Exception {
        MappedStatement ms = configuration.getMapperStatementMap().get(statementId);
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        return simpleExecutor.update(configuration, ms, params);
    }

    @Override
    public int delete(String statementId, Object params) throws Exception {
        return update(statementId, params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object object = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, (proxy, method, args) -> {

            String methodName = method.getName();
            String className = method.getDeclaringClass().getName();
            String statementId = className + "." + methodName;

            MappedStatement mappedStatement = configuration.getMapperStatementMap().get(statementId);

            Object result = null;
            switch (mappedStatement.getType()) {
                case INSERT: {
                    result = insert(statementId, args[0]);
                    break;
                }
                case UPDATE: {
                    result = update(statementId, args[0]);
                    break;
                }
                case DELETE: {
                    result = delete(statementId, args[0]);
                    break;
                }
                case SELECT:
                    Type genericReturnType = method.getGenericReturnType();
                    if (genericReturnType instanceof ParameterizedType) {
                        List<Object> objects = selectList(statementId, args);
                        return objects;
                    }
                    result = selectOne(statementId, args);
                    break;
                case FLUSH:
                    break;
                default:
                    throw new BizException("Unknown execution method for: " + mappedStatement.getType());
            }
            return result;
        });

        return (T) object;
    }
}
